package com.quan.xczs.rpc.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Condition;

import com.quan.base.common.base.service.support.BaseService;
import com.quan.xczs.rpc.api.XczsUserCouponService;
import com.quan.xczs.rpc.dao.mapper.XczsUserCouponMapper;
import com.quan.xczs.rpc.model.XczsUserCoupon;

@Service
public class XczsUserCouponServiceImpl extends BaseService<XczsUserCoupon, Integer> implements XczsUserCouponService {
	
	@Autowired
	private XczsUserCouponMapper mapper ;

	@Override
	public int deleteByParams(String mobile, String openid, Integer couponId) {
		Condition condition = new Condition(XczsUserCoupon.class) ;
		if(StringUtils.isNotBlank(mobile)) {
			condition.createCriteria().andEqualTo("xczsUserMobile", mobile);
		} else if(StringUtils.isNotBlank(mobile)) {
			condition.createCriteria().andEqualTo("xczsUserOpenid", openid);
		} else if(couponId != null) {
			condition.createCriteria().andEqualTo("xczsCouponId", couponId);
		}
		
		return this.mapper.deleteByCondition(condition);
	}

	@Override
	public XczsUserCoupon getUserCouponIdByOpenId(String openid) {
		Condition condition = new Condition(XczsUserCoupon.class) ;
		condition.createCriteria().andEqualTo("xczsUserOpenid", openid);
		List<XczsUserCoupon> selectByCondition = this.mapper.selectByCondition(condition);
		if(null != selectByCondition && selectByCondition.size() == 1) {
			return selectByCondition.get(0) ;
		} else {
			return null; 
		}
	}

}
